package com.codedancer.campus.manager.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codedancer.campus.manager.service.ILostFoundItemCommentService;
import com.codedancer.campus.manager.service.bo.lostFoundItemComment.LostFoundCommentPageSearchBo;
import com.codedancer.campus.manager.service.vo.lostFoundItemComment.LostFoundItemCommentVo;
import com.codedancer.campus.mapper.LostFoundItemCommentMapper;
import com.codedancer.campus.mapper.UserMapper;
import com.codedancer.campus.po.LostFoundItemComment;
import com.codedancer.campus.po.User;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class LostFoundItemCommentServiceImpl implements ILostFoundItemCommentService {

    @Autowired
    private LostFoundItemCommentMapper lostFoundItemCommentMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 分页查询某个物品的评论
     *
     * @param lostFoundCommentPageSearchBo
     * @return
     */
    @Override
    public IPage<LostFoundItemCommentVo> page(
        LostFoundCommentPageSearchBo lostFoundCommentPageSearchBo) {
        IPage<LostFoundItemComment> lostFoundItemCommentIPage = lostFoundItemCommentMapper.page(
            new Page<LostFoundItemComment>(lostFoundCommentPageSearchBo.getPage(),
                lostFoundCommentPageSearchBo.getSize()),
            lostFoundCommentPageSearchBo.getLostFoundItemUuid());
        IPage<LostFoundItemCommentVo> lostFoundItemCommentVoIPage = new Page<>();
        List<LostFoundItemCommentVo> lostFoundItemCommentVos;
        lostFoundItemCommentVos = lostFoundItemCommentIPage.getRecords().stream()
            .map(lostFoundItemComment -> {
                LostFoundItemCommentVo lostFoundItemCommentVo = BeanUtil.copyProperties(
                    lostFoundItemComment, LostFoundItemCommentVo.class);
                User user = userMapper.selectById(lostFoundItemComment.getCommentUserUuid());
                lostFoundItemCommentVo.setCommentNickName(user.getNickName());
                lostFoundItemCommentVo.setCommentUserAvatar(user.getAvatar());
                return lostFoundItemCommentVo;
            }).collect(Collectors.toList());
        lostFoundItemCommentVoIPage.setTotal(lostFoundItemCommentIPage.getTotal());
        lostFoundItemCommentVoIPage.setRecords(lostFoundItemCommentVos);
        return lostFoundItemCommentVoIPage;
    }
}
